package com.example.carconsumer.controller.hkf;

import com.alibaba.dubbo.config.annotation.Reference;
import com.example.carinterface.pojo.OwnerCard;
import com.example.carinterface.pojo.User;
import com.example.carinterface.service.hkf.OwnerCardService;
import com.example.carinterface.service.hkf.UserService;
import com.example.carinterface.utils.ResultJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@CrossOrigin
@Api(value = "车主卡接口", tags = "车主卡API")
@RestController
@RequestMapping("/user")
public class OwnerCardController {
    @Reference(check = false)
    OwnerCardService ownerCardService;

    @PostMapping("/intsertOwnerCard")
    @ApiOperation(value = "添加车主卡", responseReference = "添加车主卡信息", httpMethod = "post", response = ResultJson.class)
    public ResultJson selectOwnerCard(@ApiParam(name = "ownerCard", value = "车主卡的信息", required = true)OwnerCard ownerCard,
                                 @ApiParam(name = "file", value = "行驶证照片", required = false) MultipartFile file,
                                 HttpServletRequest request) {
        ResultJson resultJson=new ResultJson();
        if (file==null){ if (ownerCardService.insertOwnerCard(ownerCard)>0){
        return resultJson.setSuccess("修改成功");
    }else {
        return resultJson.setFail("修改失败");
    }}

        else {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
        String res = sdf.format(new Date());
        String name=ownerCard.getOwnerName();
        // uploads文件夹位置
            String rootPath= ClassUtils.getDefaultClassLoader().getResource("").getPath();
//        String rootPath = request.getSession().getServletContext().getRealPath("download/drivingLicense/");
        // 原始名称
        String originalFileName = file.getOriginalFilename();
        String substring = originalFileName.substring(originalFileName.lastIndexOf("."));//后缀名
        if (substring.equals(".jpg")||substring.equals(".png")){
            // 新文件名
            String newFileName = name + res + substring;
            // 创建年月文件夹
            Calendar date = Calendar.getInstance();
            File dateDirs = new File(date.get(Calendar.YEAR) + File.separator + (date.get(Calendar.MONTH) + 1));

            // 新文件
            File newFile = new File(rootPath+"/download/drivingLicense/" + File.separator + dateDirs + File.separator + newFileName);
            // 判断目标文件所在目录是否存在
            if (!newFile.getParentFile().exists()) {
                // 如果目标文件所在的目录不存在，则创建父目录
                newFile.getParentFile().mkdirs();
            }


            // 将内存中的数据写入磁盘
            try {
                file.transferTo(newFile);
            } catch (IOException e) {
                e.printStackTrace();
            }


           ownerCard.setDrivingLicenseUrl("download\\drivingLicense"+ File.separator + dateDirs + File.separator + newFileName);
            System.out.println( "download"+ File.separator + dateDirs + File.separator + newFileName);
            if (ownerCardService.insertOwnerCard(ownerCard)>0){
                return resultJson.setSuccess("修改成功");
            }else {
                return resultJson.setFail("修改失败");
            }
        }
        else {
            return resultJson.setFail("添加失败,图片文件格式错误");
        }}


}
    @GetMapping("/selectOwnerCard")
    @ApiOperation(value = "查询我的车主卡", responseReference = "查询我的车主卡信息", httpMethod = "post", response = ResultJson.class)
    public ResultJson<List> selectOwnerCard(@ApiParam(name = "userId", value = "当前用户的id", required = true)Integer userId){
        return ownerCardService.selectOwnerCard(userId);
    }
}
